﻿using MxWeiXinPF.Common;
using MxWeiXinPF.WeiXinComm;
using Senparc.Weixin.MP.AdvancedAPIs;
using Senparc.Weixin.MP.AdvancedAPIs.QrCode;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MxWeiXinPF.Web.admin.users
{
    public partial class user_qrcode : MxWeiXinPF.Web.UI.ManagePage
    {
        protected string action = MXEnums.ActionEnum.Add.ToString();
        private WeiXinCRMComm cpp = new WeiXinCRMComm();
        private int id = 0;
        private int wid = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string queryString = MXRequest.GetQueryString("action");
            MxWeiXinPF.Model.manager weiXinCode = base.GetAdminInfo();
            this.wid = weiXinCode.id;
            if (!string.IsNullOrEmpty(queryString) && (queryString == MXEnums.ActionEnum.Edit.ToString()))
            {
                this.action = MXEnums.ActionEnum.Edit.ToString();
                this.id = MXRequest.GetQueryInt("id");
                if (this.id == 0)
                {
                    base.JscriptMsg("传输参数不正确！", "back", "Error");
                }
                else if (!new MxWeiXinPF.BLL.users().Exists(this.id))
                {
                    base.JscriptMsg("信息不存在或已被删除！", "back", "Error");
                }
                else
                {
                    string error = "";
                    string accessTokenOrAppId = this.cpp.getAccessToken(39, out error);
                    if (error != "")
                    {
                        base.JscriptMsg(error, "", "Error");
                    }
                    else
                    {
                        CreateQrCodeResult result;
                        string str4 = @"Upload\users\";
                        string path = string.Concat(new object[] { HttpRuntime.AppDomainAppPath, str4, this.wid, @"\", this.id, @"\" });
                        string imgPath = path + "Qrcode.jpg";
                        if (!Directory.Exists(path))
                        {
                            Directory.CreateDirectory(path);
                            result = new CreateQrCodeResult();
                            SaveQrCodeImage(QrCodeApi.GetShowQrCodeUrl(QrCodeApi.Create(accessTokenOrAppId, 0, this.id, 0x2710).ticket), imgPath);
                            this.img.ImageUrl = imgPath;
                        }
                        else if (!System.IO.File.Exists(imgPath))
                        {
                            result = new CreateQrCodeResult();
                            SaveQrCodeImage(QrCodeApi.GetShowQrCodeUrl(QrCodeApi.Create(accessTokenOrAppId, 0, this.id, 0x2710).ticket), imgPath);
                           // this.img.ImageUrl = imgPath;
                            this.img.ImageUrl = string.Concat(new object[] { "../../upload/users/", this.wid, "/", this.id, "/Qrcode.jpg" });
                        }
                        else
                        {
                            this.img.ImageUrl = string.Concat(new object[] { "../../upload/users/", this.wid, "/", this.id, "/Qrcode.jpg" });
                        }
                    }
                }
            }
        }

        public static void SaveQrCodeImage(string qrcodeurl, string imgPath)
        {
            if (qrcodeurl == null)
            {
                throw new ArgumentNullException("qrcodeurl");
            }
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(qrcodeurl);
            request.Method = "GET";
            using (request.GetResponse())
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string address = response.ResponseUri.ToString();
                WebClient client = new WebClient();
                try
                {
                    client.DownloadFile(address, imgPath);
                }
                catch (Exception)
                {
                    throw new Exception("获取二维码图片失败！");
                }
            }
        }
    }
}